<template>
  <div class="report-wrapper">
    <div class="search-wrapper">
      <px-form ref="queryRef" class="custom-form-ui" :model="queryParams" inline label-width="90px">
        <px-form-item label="年份" prop="year">
          <px-select v-model="queryParams.year" placeholder="请选择年份">
            <px-option v-for="item in yearList" :key="item.value" :label="item.label" :value="item.value" />
          </px-select>
        </px-form-item>
        <px-form-item class="option-item">
          <px-button type="primary" @click="searchAction">查询</px-button>
          <px-button v-has="'GAS_REPORT_BUDGET_EXPORT'" type="primary" plain @click="exportAction">导出</px-button>
          <!-- <px-button @click="resetQuery">重置</px-button> -->
        </px-form-item>
      </px-form>
    </div>
    <div class="frame-wrapper">
      <iframe id="reportFrame" :src="templateUrl" width="100%" height="100%" frameborder="0" />
    </div>
    <ReTeleport>
      <div class="op-btns footer-btns">
        <div class="right"><px-button v-has="'GAS_REPORT_BUDGET_SUBMIT'" type="primary" @click="submitAction">提交</px-button></div>
      </div>
    </ReTeleport>
  </div>
</template>

<script setup lang="ts">
import { deleteReportRecord } from "@/api/report";
import { onMounted, reactive, toRefs, ref, computed, nextTick } from "vue";
import { PxMessage, PxMessageBox } from "px-ui";
import { useRouter, useRoute } from "vue-router";
import { useNav } from "@/layout/hooks/useNav";
import dayjs from "dayjs";
import ReTeleport from "@/components/ReTeleport";

defineOptions({
  name: "GAS_REPORT_BUDGET"
});

const { username } = useNav();
const router = useRouter();
const route = useRoute();

const templateUrl = ref();

const queryParams = reactive({
  year: ""
});
const yearList = ref([]);
const addLineAction = () => {
  document.getElementById("reportFrame").contentWindow.postMessage("_g().clickAddLineButton", "*");
};
const deleteLineAction = () => {
  document.getElementById("reportFrame").contentWindow.postMessage("_g().clickDeleteLineButton", "*");
};

const submitAction = () => {
  document.getElementById("reportFrame").contentWindow.postMessage("_g().verifyAndWriteReport()", "*");
};
// importExcel_Clean清空导入
// importExcel_Append 增量导入
// importExcel_Cover 覆盖导入
const importAction = () => {
  document.getElementById("reportFrame").contentWindow.postMessage("_g().importExcel_Cover()", "*");
};
const exportAction = () => {
  document.getElementById("reportFrame").contentWindow.postMessage("_g().exportReportToExcel('simple')", "*");
};

const printAction = () => {
  document.getElementById("reportFrame").contentWindow.postMessage(`_g().noClientPrint(true,true)`, "*");
};
const { VITE_FANRUAN_BASE_URL } = import.meta.env;
const getUrl = () => {
  let tpName = encodeURIComponent(encodeURIComponent("reportCenter/gas/预算管理报表.cpt"));
  let baseUrl = `${VITE_FANRUAN_BASE_URL}/webroot/decision/view/report?viewlet=${tpName}&user=${username.value}&__pi__=false&op=write&facCode=${localStorage.getItem("facCode") || ""}`;
  if (queryParams.year) {
    baseUrl = `${baseUrl}&year=${queryParams.year}`;
  }
  templateUrl.value = baseUrl;
};
onMounted(() => {
  let ym = new Date();
  let tmpList = [];
  for (var i = -8; i < 3; i++) {
    let y: number = ym.getFullYear();
    tmpList.push({
      label: y + i,
      value: y + i
    });
  }
  yearList.value = tmpList;
  if (route.query.date) {
    ym = new Date(route.query.date);
  }
  let year = ym.getFullYear();
  queryParams.year = year;
  getUrl();
});
const searchAction = () => {
  getUrl();
};
</script>
<style scoped lang="scss">
.report-wrapper {
  padding: 16px;
  overflow: hidden;
  background: #fff;
  border-radius: 4px;
}

.frame-wrapper {
  position: relative;
  height: calc(100vh - 270px);
  overflow-y: hidden;
}

.template-content {
  width: 100%;
  background-color: #fff;
}

.op-btns {
  display: flex;
  padding: 16px 0;
  background-color: #fff;

  .left {
    flex: 1;
    text-align: left;
  }

  .center {
    flex: 1;
    text-align: center;
  }

  .right {
    flex: 1;
    text-align: right;
  }
}

.footer-btns {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  width: 100%;
  height: 100%;
  padding: 16px;
}
</style>
